<?php
/**
 * @author Vanadia
 * @copyright Jiuchi Information Technology Co.,Ltd
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Default controller class
 */
class User_management extends CI_Controller {
	/**
	 * Construnct function
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		$this->load->helper('url_helper');
		$this->load->library('session');
		$uid = $this->session->userdata('admin_uid');
		if(empty($uid)){
		    redirect(site_url('admin/login'));
		}
	}
	
	public function index(){
	    $res = $this->user_model->getAllUser();
	    $data = array(
	        'users'		=> $res,
	    	'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
	    );
	    $this->load->view('admin/user_management/user_manage',$data);
	}
	
	public function add(){
		$data = array(
			'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
		);
		$this->load->view('admin/user_management/edit_user', $data);
	}
	
	public function edit_user($id){
		$userInfo = $this->user_model->getInfo($id);
		if ($userInfo) {
			$data = array(
					'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
					'userInfo' 			=> $userInfo
			);
		}else{
			redirect('admin/user_management/');
		}
		$this->load->view('admin/user_management/edit_user',$data);
	}
	
	public function updateSys_user($sys_userId){
	    $sys_userInfo = array(
	        'account'   =>$this->input->post('account'),
	        'name'     =>$this->input->post('name'),
	        'password' =>md5(md5($this->input->post('password')).'qiwi'),
	        'email' =>$this->input->post('email'),
	        'created_at'	=>	date('Y-m-d H:i:s', time())
	    );
	    if($sys_userInfo){
	        $this->sys_user_model->updateUserInfo($sys_userId,$sys_userInfo);
	        die("<script>alert('提交成功');window.location.href='".site_url('admin/sys_user_management/')."';</script>");
	    }
	}
	
	public function del_Sys_user($id)
	{
	    $res = $this->sys_user_model->updateUserInfo($id,array('deleted'=>1));
	    if($res){
	        die("<script>alert('删除成功');window.location.href='".site_url('/admin/sys_user_management/')."';</script>");
	    }
	}
}